﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhMucDAO
/// </summary>
public class DanhMucDAO
{
    DataProvider provider;
    public DanhMucDAO()
	{
        provider = new DataProvider();
	}

    public List<DanhMuc> HienThiDanhMuc()
    {
        List<DanhMuc> list = new List<DanhMuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThi", provider.Conn);
            cmd.Parameters.Add("@TenBang", SqlDbType.VarChar).Value = "DanhMuc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DanhMuc dto = new DanhMuc();
                dto.IDDanhMuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMuc"].ToString());
                dto.TenDanhMuc = ds.Tables[0].Rows[i]["TenDanhMuc"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }
    public List<DanhMuc> HienThiDanhMucDropDown()
    {
        List<DanhMuc> list = new List<DanhMuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThi", provider.Conn);
            cmd.Parameters.Add("@TenBang", SqlDbType.VarChar).Value = "DanhMuc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            DanhMuc dto = new DanhMuc();
            dto.IDDanhMuc = -1;
            dto.TenDanhMuc = "--- Tất cả mọi danh mục ---";
            list.Add(dto);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dto = new DanhMuc();
                dto.IDDanhMuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMuc"].ToString());
                dto.TenDanhMuc = ds.Tables[0].Rows[i]["TenDanhMuc"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public DanhMuc HienThiChiTietDanhMuc(int id)
    {
        DanhMuc dto = new DanhMuc();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiChiTietDanhMuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {               
                dto.IDDanhMuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMuc"].ToString());
                dto.TenDanhMuc = ds.Tables[0].Rows[i]["TenDanhMuc"].ToString();
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return dto;
    }

    public int ThemDanhMuc(string tenDM)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemDanhMuc", provider.Conn);
            cmd.Parameters.Add("@Ten", SqlDbType.NVarChar).Value = tenDM;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 0;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }

    public int CapNhatDanhMuc(int id, string tenDM)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemDanhMuc", provider.Conn);
            cmd.Parameters.Add("@Ten", SqlDbType.NVarChar).Value = tenDM;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }

    public int XoaDanhMuc(int id)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_XoaDanhMuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
}